<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import * as dd from "dingtalk-jsapi";
import {
  getAccessToken,
  getUser,
  getUserInfo,
  ContainsPhoneNumber,
} from "@/api/index";

export default {
  // watch: {
  //   accessToken(newVal) {
  //     if (newVal && this.code) {
  //       this.onGetUser();
  //     }
  //   },
  //   code(newVal) {
  //     if (newVal && this.accessToken) {
  //       this.onGetUser();
  //     }
  //   },
  // },
  data() {
    return {
      code: "",
      accessToken: "",
      userId: {},
      userInfo: {},
      isPhone: false,
    };
  },
  created() {
    // this.getIsPhone();
    // this.getDingAPI();
    this.$router.beforeEach((to, from, next) => { 
      if (!this.$store.state.user.userCode && to.path !=='/') {
        this.$message.warning("请登录！！！")
        return
      }
      next()
    })
  },
  methods: {
    // 获取是否为手机端
    getIsPhone() {
      let info = navigator.userAgent;
      this.isPhone = /mobile/i.test(info);
      this.$store.commit("user/setUserIsPhone", this.isPhone);
    },

    // 链接钉钉Api,获取通行证
    async getDingAPI() {
      console.log("引入钉钉---->", dd.env.platform);
      if (dd.env.platform !== "notInDingTalk") {
        let _this = this;
        dd.ready(function () {
          // dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
          dd.runtime.permission.requestAuthCode({
            corpId: "ding546527c2838da0e9",
            onSuccess: function (result) {
              // console.log('第一步：获取code',result.code)
              _this.code = result.code;
            },
            onFail: function (err) {
              console.log("dd.onFail", err);
            },
          });
        });
        // 获取token
        let token = await getAccessToken({
          appKey: "dinguorjkehhr1mhx7ui",
          appSecret:
            "9wtjy6eKbt5zP-Qky3qtvWZ-rJ5odNCCCrAeKkrwzWmDLGxNtNyx5OJM937LzIz9",
        });
        // console.log('第二步：获取token',token.accessToken)
        this.accessToken = token.accessToken;
        console.log("accessToken--------->", this.accessToken);
        this.onGetUser();
      }
    },

    // 获取用户id
    async onGetUser() {
      let result = await getUser(
        {
          access_token: this.accessToken,
        },
        {
          code: this.code,
        }
      );
      // console.log('第三步：获取用户信息',result)
      this.userId = result.result;
      this.onGetUserInfo();
    },

    // 获取用户信息
    async onGetUserInfo() {
      let result = await getUserInfo(
        {
          access_token: this.accessToken,
        },
        {
          userid: this.userId.userid,
          language: "zh_CN",
        }
      );
      // console.log('第四',result)
      this.userInfo = result.result;
      let payload = {
        userId: this.userId,
        userInfo: this.userInfo,
      };
      console.log("payload----->", payload);
      this.$store.commit("user/setDingUserData", this.userId);
      console.log("userId---------", this.userId);
      this.$store.commit("user/setDingUserInfoData", this.userInfo);
      console.log("dingUserInfo----->", this.$store.state.user.dingUserInfo);
      if (!this.$store.state.user.userIsPhone) {
        if (this.$route.fullPath !== "/tablePage") {
          this.$router.replace({
            name: "tablePage",
            params: {
              id: this.userId.userid,
              name: this.userId.name,
            },
          });
        }
      }
      // let whiteResult = await ContainsPhoneNumber({
      //   phoneNumber: this.userInfo.mobile,
      // });
      // this.isWhiteUser = whiteResult.result;
    },
  },
};
</script>
